﻿namespace EFS.RC.CC.CH.Tables
{
	public static class ADUsers
	{
		/// Create
		public static void CreateIfNotExists(
			System.Guid userGuid,
			System.String logon,
			System.String firstName,
			System.String lastName)
		{
			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// parameters array
				EFS.DB.Parameters.Array parametersArray = new EFS.DB.Parameters.Array(
						new System.String[] { "@object_guid", "@logon_name", "@first_name", "@last_name" },
						new System.Object[] { userGuid, logon, firstName, lastName },
						new System.Boolean[] { false, false, false, false });

				// execute
				connection.DoExecuteNonQuery("[dbo].[ADUsers_CreateIfNotExists]", parametersArray);
			}
		}

		/// Create
		public static System.Int32 GetIDByLogonName(
			System.String logon)
		{
			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// parameters array
				EFS.DB.Parameters.Array parametersArray = new EFS.DB.Parameters.Array(
						new System.String[] { "@logon_name" },
						new System.Object[] { logon },
						new System.Boolean[] { false });

				// exec create
				using (System.Data.DataSet resultDataSet = connection.DoExecuteQuery("[dbo].[ADUsers_GetByLogonName]", parametersArray))
				{
					if (resultDataSet.Tables.Count > 0)
					{
						// get tables
						using (System.Data.DataTable table = resultDataSet.Tables[0])
						{
							if (table.Rows.Count > 0)
							{
								return EFS.DB.DataRow.Int32(table.Rows[0], "id").Value;
							}
						}
					}
				}

				// none
				return 0;

			}
		}
	}
}